package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actsigncharge;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;
import java.util.Map;

public class ActiveModule_每日累冲 extends PlayerModule {

	//bean -------------------------------------------
	public static final int acttype = 3;   
	
	private int payexp;			//活动时间内充值经验
	private DateTime updatetime;	//更新时间     一天一刷
	private int payamounts;		//实际充值金额  单位分
	private ArrayList<Integer> cangetids;	//可以领取的id组
	private ArrayList<Integer> getids;		//已经领取的id组

	public int getPayexp() {
		return payexp;
	}
	public void setPayexp(int payexp) {
		this.payexp = payexp;
	}
	public DateTime getUpdatetime() {
		return updatetime;
	}
	public void setUpdatetime(DateTime updatetime) {
		this.updatetime = updatetime;
	}
	public int getPayamounts() {
		return payamounts;
	}
	public void setPayamounts(int payamounts) {
		this.payamounts = payamounts;
	}
	public ArrayList<Integer> getCangetids() {
		return cangetids;
	}
	public void setCangetids(ArrayList<Integer> cangetids) {
		this.cangetids = cangetids;
	}
	public ArrayList<Integer> getGetids() {
		return getids;
	}
	public void setGetids(ArrayList<Integer> getids) {
		this.getids = getids;
	}
	
	//func -------------------------------------------

	/**
	 * 初始化
	 * @param
	 * @param sai
	 * @return
	 */
	static public ActiveModule_每日累冲 create(String playerCode, activeControl sai) {
		ActiveModule_每日累冲 am = new ActiveModule_每日累冲();
		
		am.setPlayerCode(playerCode);
		am.setPayexp(0);
		am.setUpdatetime(new DateTime(0));
		am.setCangetids(new ArrayList<Integer>());
		am.setGetids(new ArrayList<Integer>());
		am.setTimeline(sai.getTimeline());
		am.set();
		
		return am;
	}
	
	/**
	 * 能否领取某个id的奖励
	 * @param id
	 * @return
	 */
	public int cangain(int id) {
		if(getCangetids().contains(id)) {
			return DrawStatus.canget.getStatus();
		}
		
		if(getGetids().contains(id)) {
			return DrawStatus.hadget.getStatus();
		}
		
		actsigncharge asc = ActDataCenter.getData(id, actsigncharge.class, getPlayerCode());
		if(getPayexp() >= asc.getCondition()) {
			getCangetids().add(asc.getId());
			this.set();
			return DrawStatus.canget.getStatus();
		}
		
		return DrawStatus.cannotget.getStatus();
	}
	
	/**
	 * 获取奖励
	 * @param id
	 * @return
	 */
	public ArrayList<Currency> reward(int id){
		actsigncharge asc = ActDataCenter.getData(id, actsigncharge.class, getPlayerCode());
		ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(asc.getRewardtype(), asc.getRewardid(), asc.getRewardnum());
		return curs;
	}
	
	/**
	 * 获取奖励后处理
	 * @param id
	 */
	public void handledrawreward(int id) {
		if(!getGetids().contains(id)) {
			getGetids().add(id);
		}
		
		if(getCangetids().contains(id)) {
			getCangetids().remove((Object)id);
		}
		
		this.set();
	}
	
	/**
	 * 添加充值经验
	 * @param addpayexp
	 */
	public void addpayvipexp(int addpayexp, int payamount) {
		int payvipexp = getPayexp();
		payvipexp = payvipexp + addpayexp;
		
		int payamounts = getPayamounts();
		payamounts = payamounts + payamount;
		
		Map<String, actsigncharge> ascmap = ActDataCenter.getAllData(actsigncharge.class, getPlayerCode());
		for(String key:ascmap.keySet()) {
			actsigncharge asc = ascmap.get(key);
			if(payamounts >= asc.getChargecondition()) {
				if(!getGetids().contains(asc.getId())) {
					if(!getCangetids().contains(asc.getId())) {
						getCangetids().add(asc.getId());
					}
				}
			}
		}
		
		setPayexp(payvipexp);
		setPayamounts(payamounts);
		this.set();
	}
	
	
	public void update() {
		AreaBean ab = AreaModule.area(getPlayerCode());
		activeControl sai = activeControl.getAreaOneActionInfo(String.valueOf(ab.getAreaId()), String.valueOf(acttype));
		if(sai == null) {
			return;
		}
		
		//关闭活动
		if(!sai.running() && running()) {
			setTimeline(sai.getTimeline());
			set();
		}
		
		//开启活动
		if(sai.running() && !running()) {
			setTimeline(sai.getTimeline());
			setCangetids(new ArrayList<Integer>());
			setGetids(new ArrayList<Integer>());
			set();
		}
				
		if(running()) {
			DateTime old = getUpdatetime();
			DateTime now = DateTime.now();
			
			int days = ActiveUtil.days(old, now);
			
			//转天刷新
			if(days >= 1) {			
				setPayexp(0);
				setPayamounts(0);
				setUpdatetime(DateTime.now());
				setCangetids(new ArrayList<Integer>());
				setGetids(new ArrayList<Integer>());
				this.set();
			}
		}
	
	}

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
	
	//redis ------------------------------------------
	
	static private String key(String pcode) {
		return String.format("PLAYER:%s:ACTIVE:%S", pcode, acttype);
	}

	public void set() {
		try {
			String key1 = key(getPlayerCode());
			setv(key1, this);
			check();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static public ActiveModule_每日累冲 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_每日累冲 module = getv(key(playerCode));

			//新用户不需要去查数据库
			if (module == null && !isNewPlayer) {
				module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_每日累冲.class);
				if(module != null){module.set();}//同步到redis
			}

			if (module == null) {
				AreaBean ab = AreaModule.area(playerCode);
				activeControl sai = activeControl.get(String.valueOf(ab.getAreaId()), String.valueOf(acttype));
				if (sai != null) {
					module = create(playerCode, sai);
				}
			}
			if(module == null){return null;}

			module.update();
			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		if(!running()) {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.关闭);
			new TipHandle().updateTip(getPlayerCode(), TipType.活动_每日累冲奖励, false);
			return;
		}else {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
		}
		ArrayList<Integer> cangets = getCangetids();
		ArrayList<Integer> hadgets = getGetids();
		
		for(int id : cangets) {
			if(!hadgets.contains(id)) {
				new TipHandle().updateTip(getPlayerCode(), TipType.活动_每日累冲奖励, true);
				return;
			}
		}

		new TipHandle().updateTip(getPlayerCode(), TipType.活动_每日累冲奖励, false);
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_每日累冲 activeModule_每日累冲 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_每日累冲);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	} 
	

}

